package com.gopay.bis.service.bank.impl.ebank;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;

import com.gopay.bis.common.util.BankLogUtils;
import com.gopay.bis.common.util.BisPayCheckUtils;
import com.gopay.bis.common.util.constants.BisPayConstants;
import com.gopay.bis.manager.bank.ebank.PabB2cManager;
import com.gopay.common.Result;
import com.gopay.common.constants.bank.PabConstants;
import com.gopay.common.constants.proccode.ProcCodeConstants;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.pab.ebank.PabB2cMessage;
import com.gopay.common.domain.bank.pab.ebank.PabOrderMessageRes;
import com.gopay.common.domain.bank.pab.ebank.PabQueryOrdersMessageRes;
import com.gopay.common.exception.GopayException;
import com.gopay.remote.bank.BankInteractRemoteService;
/**
 * 平安银行B2C支付接口改造
 * 
 *
 * @author 宣广海
 *
 * @version 
 *
 * @since 2016年4月13日
 */
public class PabB2cBankServiceImpl  implements BankInteractRemoteService {
	/**
	 * 注入对象
	 */
	@Resource(name = "pabB2cManager")
	private PabB2cManager pabB2cManager;
	
	@Resource(name = "pabConfig")
	private Map<String, String> pabConfig;
	
	/**
     * 支付类请求
     * 
     * @param BaseMessage 
     * @return Result
     */
	public Result pay(BaseMessage base) {
		Result result = new Result(false);
		try {
			PabB2cMessage messageB2C = new PabB2cMessage();
			BeanUtils.copyProperties(base, messageB2C);
			/**
			 * 支付接口签名
			 */
			messageB2C = (PabB2cMessage) pabB2cManager.sign(messageB2C);
			base.setBankOrderId(base.getOrderId());
			result.setDefaultModel(messageB2C);
			result.setSuccess(true);
		} catch (GopayException e) {
			BankLogUtils.printServiceImplExceptionLog(e);
			result.setResultCode(e.getErrCode());
		} catch (Exception ex) {
			BankLogUtils.printServiceImplExceptionLog(ex);
			result.setResultCode(ProcCodeConstants.PROC_CODE_100E6029);
		}
		return result;
	}
	
	@Override
	public Result bankBack(BaseMessage base) {
		Result result = new Result(false);
		try {
			PabOrderMessageRes messageB2C = new PabOrderMessageRes();
			BeanUtils.copyProperties(base, messageB2C);
			boolean flag = (Boolean) pabB2cManager.verify(messageB2C);
			result.setDefaultModel(flag);
			result.setSuccess(true);
			result.setModel("res", messageB2C);
			Map<String, String> param = new HashMap<String, String>();
			// 验签成功入账
			if (flag) {
				if (messageB2C.getCommitFlag().equals("1")) {
					// 通知后台地址入账
					param.put("orderId", messageB2C.getOrderId());
					param.put("amt", messageB2C.getAmount().toString());
					param.put("cardType", "Unknown");
					param.put("notifyNum", "1");
					param.put("payStatus", "00");
					param.put("retCode", "00");
					param.put("retMsg", "验签成功");
					param.put("signData", messageB2C.getSignMessage());
					param.put("isFront", base.isFront()+"");
					param.put("jumpURL",pabConfig.get("PAB.B2C.RETURN.URL"));
					result.setModel("isNotifyCashier", param);
					result.setOrderStatus(true);
				} else {
					// 通知后台地址该订单失败					
					param.put("orderId", messageB2C.getOrderId());
					param.put("amt", messageB2C.getAmount().toString());
					param.put("cardType", "Unknown");
					param.put("notifyNum", "1");
					param.put("payStatus", "01");
					param.put("retCode", "01");
					param.put("retMsg", "验签失败");
					param.put("signData", messageB2C.getSignMessage());
					param.put("isFront", base.isFront()+"");
					result.setModel("isNotifyCashier", param);
					result.setOrderStatus(false);
				}
			}else {
				// 通知验签失败					
				param.put("orderId", messageB2C.getOrderId());
				param.put("amt", messageB2C.getAmount().toString());
				param.put("cardType", "Unknown");
				param.put("notifyNum", "1");
				param.put("payStatus", "01");
				param.put("retCode", "01");
				param.put("retMsg", "验签失败");
				param.put("signData", messageB2C.getSignMessage());
				param.put("isFront", base.isFront()+"");
				result.setModel("isNotifyCashier", param);
				result.setOrderStatus(false);
			}
		} catch (GopayException e) {
			BankLogUtils.printServiceImplExceptionLog(e);
			result.setResultCode(e.getErrCode());
		} catch (Exception ex) {
			BankLogUtils.printServiceImplExceptionLog(ex);
			result.setResultCode(ProcCodeConstants.PROC_CODE_100E6064);
		}
		return result;
	}
	
	 /**
     * 银行查询类请求
     * 
     * @param BaseMessage
     * @return Result
     */
	public Result query(BaseMessage base) {
		Result result = new Result(false);
		try {
			PabB2cMessage messageB2C = new PabB2cMessage();
			BeanUtils.copyProperties(base, messageB2C);
			PabQueryOrdersMessageRes res = (PabQueryOrdersMessageRes) pabB2cManager.query(messageB2C);
			result.setDefaultModel(res);
			result.setSuccess(true);
			if(BisPayCheckUtils.checkExternalOrderID(messageB2C.getOrderId())){
				Map<String, String> param = new HashMap<String, String>();
				param.put("orderId", messageB2C.getOrderId());
				param.put("amount", messageB2C.getAmount().toString());
				if (res == null) {
					// 查询订单为空
					param.put("returnCode", BisPayConstants.QUERYSTATE_FAIL);
					param.put("returnMessage", "银行返回对账结果为空");
					param.put("tradeState", BisPayConstants.TRADESTATE_OTHER);
					result.setModel("queryResult", param);
					return result;
				}
				String commitFlag = res.getStatus();// 1：成功2：失败3: 支付结果未知
				// 成功处理
				if (PabConstants.TRAN_SDB_DEAL_CHECK_IN_NEW.equals(commitFlag)) {
					param.put("returnCode", BisPayConstants.QUERYSTATE_SUCCESS);
					param.put("tradeState", BisPayConstants.TRADESTATE_SUCCESS);
					param.put("returnMessage", "交易成功");
				} else if(PabConstants.TRAN_SDB_DEAL_CHECKOUT_NEW.equals(commitFlag)){
					param.put("returnCode", BisPayConstants.QUERYSTATE_SUCCESS);
					param.put("tradeState", BisPayConstants.TRADESTATE_FAIL);
					param.put("returnMessage", PabConstants.TRAN_SDB_MESSAGE_DEAL_CHECKOUT_NEW);
				}else if(PabConstants.TRAN_SDB_DEAL_DONT_CHECK_NEW.equals(commitFlag)){
					param.put("returnCode", BisPayConstants.QUERYSTATE_SUCCESS);
					param.put("tradeState", BisPayConstants.TRADESTATE_OTHER);
					param.put("returnMessage", PabConstants.TRAN_SDB_MESSAGE_DEAL_DONT_CHECK_NEW);
				}
				else {
					param.put("returnCode", BisPayConstants.QUERYSTATE_SUCCESS);
					param.put("tradeState", BisPayConstants.TRADESTATE_OTHER);
					param.put("returnMessage", "对账成功，暂不进行账务处理");
				}
				result.setModel("queryResult", param);
			}
		} catch (GopayException e) {
			BankLogUtils.printServiceImplExceptionLog(e);
			result.setResultCode(e.getErrCode());
		} catch (Exception ex) {
			BankLogUtils.printServiceImplExceptionLog(ex);
			result.setResultCode(ProcCodeConstants.PROC_CODE_100E6033);
		}		
		return result;
	}
	@Override
	public Result refund(BaseMessage base) {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public Result dayend(BaseMessage base) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Result queryBack(BaseMessage base) {
		// TODO Auto-generated method stub
		return null;
	}

    @Override
    public Result refundBack(BaseMessage base) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Result refundQuery(BaseMessage base) {
        // TODO Auto-generated method stub
        return null;
    }

	@Override
	public Result sendBankRefund(BaseMessage base) {
		// TODO Auto-generated method stub
		return null;
	}	

	public void setPabConfig(Map<String, String> pabConfig) {
		this.pabConfig = pabConfig;
	}
}
